﻿using System;
using Microsoft.Win32;

namespace BrowserMaster
{
    public class Registrar
    {
        #region Methods

        public static void Register(string protocol, string handlerPath, string iconPath)
        {
            //var root = Registry.ClassesRoot;
            //var root = Registry.CurrentUser.OpenSubKey(@"Software\Classes\", true);
            CreateClass();
            //Register(root, protocol, handlerPath, iconPath);
        }

        public static void CreateClass()
        {
            string masterName = "BMaster";
            string baseFolder = @"SOFTWARE\Classes\" + masterName;
            string iconFolder = "DefaultIcon";
            string shellFolder = "shell";
            string openFolder =  "open";
            string command = "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\" %1";
            string defaultIcon = System.Reflection.Assembly.GetExecutingAssembly().Location + ",0";
            
            RegistryKey root = Registry.LocalMachine;
            RegistryKey baseName = root.CreateSubKey(baseFolder);

            RegistryKey icon = baseName.CreateSubKey(iconFolder);
            icon.SetValue( "",defaultIcon);            
            
            RegistryKey shell = baseName.CreateSubKey(shellFolder);

            RegistryKey openkey = shell.CreateSubKey(openFolder);
            RegistryKey commandKey = openkey.CreateSubKey("command");
            commandKey.SetValue("", command);
            
            //clean up
            commandKey.Close();
            icon.Close();
            openkey.Close();
            shell.Close();
            baseName.Close();
            root.Close();

            //register the program with the OS (Win 7)
            //HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
            root = Registry.CurrentUser;
            RegistryKey userChoiceHTTP = root.CreateSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice");
            userChoiceHTTP.SetValue("Progid", masterName);
            RegistryKey userChoiceHTTPS = root.CreateSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice");
            userChoiceHTTPS.SetValue("Progid", masterName);
            RegistryKey userChoiceFTP = root.CreateSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice");
            userChoiceFTP.SetValue("Progid", masterName);
            
            //clean up
            root.Close();
            userChoiceFTP.Close();
            userChoiceHTTP.Close();
            userChoiceHTTPS.Close();

        }

        public static void Register(RegistryKey root, string protocol, string handlerPath, string iconPath)
        {
            protocol = protocol.ToLower();

            // If this key already exists delete it so we start fresh
            try
            {
                root.DeleteSubKeyTree(protocol);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }  
         
            RegistryKey protocolKey = root.CreateSubKey(protocol);
            protocolKey.SetValue(@"URL Protocol", string.Empty, RegistryValueKind.String);
            protocolKey.SetValue(string.Empty, @"URL:HyperText Transfer Protocol");

            RegistryKey cmdKey = OpenOrCreateSubKey(protocolKey, @"shell\open\command");
            cmdKey.SetValue(string.Empty, handlerPath);

            if (string.IsNullOrEmpty(iconPath)) return;
            RegistryKey iconKey = OpenOrCreateSubKey(protocolKey, @"DefaultIcon");
            iconKey.SetValue(string.Empty, iconPath);
        }

        /// <summary>
        /// Registers the application at the path specified as the default browser.
        /// </summary>
        /// <param name="path">The path to the browser with the arguments specified</param>
        public static void RegisterBrowser(string path, string iconPath)
        {
            if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
            if (!path.Contains(@"%1")) throw new ArgumentException("Path must specify the full argument list and contain %1 to represent passed in arguments");
            Register("http", path, iconPath);
            Register("https", path, iconPath);
        }

        #endregion

        #region Helpers
        private static RegistryKey OpenOrCreateSubKey(RegistryKey baseKey, string subKeyPath)
        {
            return baseKey.OpenSubKey(subKeyPath, true) ?? baseKey.CreateSubKey(subKeyPath);
        }
        #endregion
    }
}
